import { ref, onMounted, computed } from 'vue'

export default function useSafeArea() {
    // 状态栏高度（刘海屏高度）
    const statusBarHeight = ref(0)
    // 导航栏高度（包括胶囊按钮区域）
    const navBarHeight = ref(44)
    // 整个安全区域高度（状态栏+导航栏）
    const safeAreaHeight = ref(0)
    // 菜单按钮信息（微信小程序特有）
    const menuButtonInfo = ref(null)

    // 获取系统信息
    const getSystemInfo = () => {
        const systemInfo = uni.getSystemInfoSync()
        statusBarHeight.value = systemInfo.statusBarHeight

        // 微信小程序环境下获取菜单按钮信息
        // #ifdef MP-WEIXIN
        menuButtonInfo.value = uni.getMenuButtonBoundingClientRect()
        navBarHeight.value = (menuButtonInfo.value.top - statusBarHeight.value) * 2 + menuButtonInfo.value.height
        // #endif

        safeAreaHeight.value = statusBarHeight.value + navBarHeight.value
    }

    // 组件挂载时获取信息
    onMounted(() => {
        getSystemInfo()
    })

    return {
        statusBarHeight,
        navBarHeight,
        safeAreaHeight,
        menuButtonInfo,
        getSystemInfo // 暴露方法以便手动刷新
    }
}